package com.betterjr.modules.risk.loan.service;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;

import com.betterjr.common.utils.BTAssert;
import com.betterjr.common.utils.Collections3;
import com.betterjr.modules.agreement.service.ScfElecAgreementService;
import com.betterjr.modules.risk.loan.dao.LoanProjectSynchInterfaces;
import com.betterjr.modules.risk.loan.data.LoanProjectElcAgreementConstantCollections;
import com.betterjr.modules.risk.loan.entity.LoanProjectRelation;

public class OthersElcAgreementLoanProjectSynchService implements LoanProjectSynchInterfaces {

    @Autowired
    private ScfElecAgreementService elcAgreementService;

    @Override
    public void InvokeDeleted(LoanProjectRelation anLoan) {
        loggerin.debug(" OthersElcAgreementLoanProjectSynchService  InvokeDeleted " + anLoan);

        try {
            elcAgreementService.saveAndCancelElecLoanAgreement(anLoan.getId(), "14");
        }
        catch (Exception e) {
            BTAssert.notNull(null, "取消其他协议异常" + e.getMessage());
        }

    }

    @Override
    public void InvokeEffective(LoanProjectRelation anLoan) {

        loggerin.debug(" OthersElcAgreementLoanProjectSynchService  InvokeEffective " + anLoan);

        try {
            if (anLoan != null && !Collections3.isEmpty(anLoan.getOtherAgreements())
                    && LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_SUBTYPE_ONLINE
                            .equals(Collections3.getFirst(anLoan.getOtherAgreements()).getSubType())
                    && Collections3.getFirst(anLoan.getOtherAgreements()).getContractTemplateId() != null) {
                elcAgreementService.saveAddElecAgreementByLoanProjectRelation(anLoan,
                        LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_BUSINTYPE_OTHERAGREEMENTS,
                        Arrays.asList(anLoan.getCustNo(), anLoan.getFactoryNo()),
                        anLoan.getFactoryName() + "与其他" + anLoan.getCustName() + "电子合同");

            }
        }
        catch (Exception e) {
            BTAssert.notNull(null, "创建其他协议异常" + e.getMessage());
        }

    }

}
